package com.link.play.fit.ai.ui.exercise

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import androidx.activity.addCallback
import androidx.core.text.isDigitsOnly
import androidx.core.widget.addTextChangedListener
import com.google.android.material.tabs.TabLayout
import com.google.gson.Gson
import com.jaygoo.widget.OnRangeChangedListener
import com.jaygoo.widget.RangeSeekBar
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.bean.ExerciseBean
import com.link.play.fit.ai.bean.RecentlyEatenData
import com.link.play.fit.ai.databinding.AcRunBinding
import com.link.play.fit.ai.databinding.ItemTabMinsBinding
import com.link.play.fit.ai.ui.main.MainAc
import com.link.play.fit.ai.utils.EventUtils


class RunActivity : BaseActivity<AcRunBinding>(AcRunBinding::inflate) {

    val exerciseBean = ExerciseBean(0, describe = "run")

    var selectIndex = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        val dataR = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getSerializableExtra("data", RecentlyEatenData::class.java)
        } else {
            @Suppress("DEPRECATION")
            (intent.getSerializableExtra("data") as RecentlyEatenData?)
        }
        dataR?.let {
            exerciseBean.intensityPercent = it.intensityPercent
            exerciseBean.time = it.time
            exerciseBean.modify = 1
        }


        onBackPressedDispatcher.addCallback {
            finish()
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.verticalRangeSeekBar.setProgress(exerciseBean.intensityPercent.toFloat())


        val colorS = Color.parseColor("#FF1D1A23")
        val colorN = Color.parseColor("#FF8D8C92")


        binding.verticalRangeSeekBar.setOnRangeChangedListener(object : OnRangeChangedListener {
            override fun onRangeChanged(view: RangeSeekBar?, leftValue: Float, rightValue: Float, isFromUser: Boolean) {

                val percent = leftValue.toInt()
                Log.e(TAG, "percent=$percent")

                when (percent) {

                    in 0..33 -> {
                        binding.tvHigh.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
                        binding.tvHighContent.setTextColor(colorN)

                        binding.tvMedium.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
                        binding.tvMediumContent.setTextColor(colorN)

                        binding.tvLow.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17f)
                        binding.tvLowContent.setTextColor(colorS)
                    }

                    in 33..66 -> {
                        binding.tvHigh.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
                        binding.tvHighContent.setTextColor(colorN)

                        binding.tvMedium.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17f)
                        binding.tvMediumContent.setTextColor(colorS)

                        binding.tvLow.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
                        binding.tvLowContent.setTextColor(colorN)
                    }

                    in 66..100 -> {
                        binding.tvHigh.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17f)
                        binding.tvHighContent.setTextColor(colorS)

                        binding.tvMedium.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
                        binding.tvMediumContent.setTextColor(colorN)

                        binding.tvLow.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
                        binding.tvLowContent.setTextColor(colorN)
                    }

                }
                exerciseBean.intensityPercent = percent
            }

            override fun onStartTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) {
            }

            override fun onStopTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) {
            }
        })

        arrayOf("15 mins", "30 mins", "60 mins", "90 mins").forEachIndexed { index, mins ->

            val tab = binding.tabLayout.newTab()

            val view = LayoutInflater.from(this@RunActivity).inflate(R.layout.item_tab_mins, null)
            val minBinding = ItemTabMinsBinding.bind(view)
            minBinding.tv.text = mins
            tab.customView = minBinding.root
            tab.id = index

            binding.tabLayout.addTab(tab)

        }

        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            @SuppressLint("SetTextI18n")
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.let {
                    it.customView?.isSelected = true
                    selectIndex = it.id
                    when (it.id) {
                        0 -> {
                            binding.edit.setText("15")
                            exerciseBean.time = 15
                        }

                        1 -> {
                            binding.edit.setText("30")
                            exerciseBean.time = 30
                        }

                        2 -> {
                            binding.edit.setText("60")
                            exerciseBean.time = 60
                        }

                        3 -> {
                            binding.edit.setText("90")
                            exerciseBean.time = 90
                        }
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                tab?.customView?.isSelected = false
            }

            override fun onTabReselected(tab: TabLayout.Tab?) = Unit
        })

        val actionSelectTab = {
            val index = when (exerciseBean.time) {
                15 -> 0
                30 -> 1
                60 -> 2
                90 -> 3
                else -> -1
            }
            if (index >= 0) {
                binding.tabLayout.selectTab(binding.tabLayout.getTabAt(index))
            } else {
                binding.tabLayout.selectTab(null)
            }
        }
        actionSelectTab.invoke()
        binding.edit.setText(exerciseBean.time.toString())

        binding.edit.addTextChangedListener {
            if (it?.isNotBlank() == true && it.isDigitsOnly()) {
                exerciseBean.time = it.toString().toInt()
                actionSelectTab.invoke()
            }
        }

        binding.clMore.setOnClickListener {
            binding.deleteText.visibility = View.VISIBLE
            binding.deleteText.setOnClickListener {
                exerciseBean.modify = -1
                setResult(Activity.RESULT_OK, Intent().apply {
                    putExtra("Exercise", exerciseBean)
                })
                finish()
            }
        }
        binding.tvAdd.setOnClickListener {
            if (exerciseBean.modify == 0) {
                EventUtils.event("log_exercise_run")
                goToAc(MainAc::class.java) {
                    putExtra("Exercise", Gson().toJson(exerciseBean))
                    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                }
            } else {
                setResult(Activity.RESULT_OK, Intent().apply {
                    putExtra("Exercise", exerciseBean)
                })
            }
            finish()
        }


    }

}